package com.sx.core;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.List;
import java.util.Vector;

public class DBUtil {
	public static void rollback(Connection conn){
		try {
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static Connection getJdbcConnection(){
		DBConfig dbc = new DBConfig();
		return dbc.getJdbcConnection();
	}
	public static Connection getConnection(){
		DBConfig dbc = new DBConfig();
		return dbc.getConnection();
	}
	public static void close(ResultSet rs,Statement pst,Connection con){
		DBConfig dbc = new DBConfig();
		dbc.close(rs, pst, con);
	}
	
	/**
	 *<pre>公有方法 关闭Statement数据库连接方�法</pre> 
	 * @return void
	 * @author LQZ 
	 * @date 2009-12-19
	 * */
	public  static void close(ResultSet rs,PreparedStatement st,Connection con)
	{
		try {
			if (rs!=null) {rs.close();}
			rs = null;
		} catch (Exception e) {
			rs = null;
			e.printStackTrace();
		}
		try {
			if (st!=null) {st.close();}
			st = null;
		} catch (Exception e) {
			st = null;
			e.printStackTrace();
		}
		try {
			if(con!=null){con.close();}
			con = null;
		} catch (Exception e) {
			con = null;
			e.printStackTrace();
		}
	}
	
	
	public static boolean execBeansBySql(String sql)
	{
		boolean flag = false;
		Connection con = getConnection();
		PreparedStatement pst = null;
		try {
			pst = con.prepareStatement(sql);
			int count = pst.executeUpdate();
			if (count>0) {
				flag = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			close(null, pst, con);
		}
		return flag;
	}
	
	public static boolean execBeansBySqls(List<String> sqls)
	{
		Connection con = getConnection();
		Statement st = null;
		try {
			st = con.createStatement();
			for (String sql : sqls) {
				st.addBatch(sql);
			}
			st.executeBatch();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally{
			close(null,st,con);
		}
	}
	
	/**COL1+'~'+COL2
     * 从源表中检索数据返回一个数据集的Vector形式(以记录集的列为Vector的每一个元素)<br>
     *实例：<br>
     *    DBUtil db=new DBUtil();<br>
     *    Vector rs=new Vector();<br>
     *    String sql="SELECT * FROM PERSON where name='li'";<br>
     *    rs=select(sql,false);<br>
     *    Enumeration e=rs.elements() ;<br>
     *    while (e.hasMoreElements())<br><br>
     *    {<br>
     *      out.println((String)e.nextElement()) ;<br>
     *    }<br>
     * 返回结果：1^ li^ 24<br>
     *@createdate:2003-11-13
     *@param: sqlStr 产生列表的SQL语句，可以为动态语句也可以为数据库内存查询语句的id
     *@param: flag true表示利用数据库内存语句查询；false表示利用动态语句执行查询
     *@return: 数据集的Vector形式，默认以"^"号分割每个字段
     **/
  public static Vector selectByCol(Connection dbConn , String sqlStr, boolean flag) {
      Vector v_ResultSet = new Vector();
      Statement stmt = null;
      ResultSet result = null;
      ResultSetMetaData rsMeta = null;
      if (!flag) {
        try {
          if(dbConn==null)  throw new Exception("Connection Is Null!"); 
          stmt = dbConn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                                        ResultSet.CONCUR_READ_ONLY);
          result = stmt.executeQuery(sqlStr);
          //v_ResultSet = getValues(result);
          int colNum=0;
          String s_TempString = "";

          rsMeta = result.getMetaData();
          colNum = rsMeta.getColumnCount();
          int i;
          for (i = 1; i <= colNum; i++){
            result.beforeFirst();
            s_TempString = "";
            while (result.next()) {
              s_TempString=s_TempString+result.getString(i)+"^";
            }
            if(!s_TempString.equals(""))
               s_TempString=s_TempString.substring(0,s_TempString.length()-1);
            v_ResultSet.addElement(s_TempString);
          }
        }
        catch (Exception ex) {
          System.out.println("CommonADO.select() ...... fengyu ...... ex : " +
                             ex.toString());
          ex.printStackTrace();
        }
        finally {
          try {
            rsMeta=null;
            result.close();
            result=null;
            stmt.close();
            stmt = null;
           ///// //db.closeDBConnection(dbConn);
            //dbConn.close();
            //dbConn=null;
          }
          catch (Exception ex) {
            System.out.println("CommonADO.select()closeclosecloseclose ...... fengyu ...... ex : " +
                               ex.toString());
            ex.printStackTrace();
          }

        }
      }
      else { //此函数待写！！！
      }
      return v_ResultSet;
    }
	
}




















